import logging
import os
import shutil
import sys
import unittest

import pdf
import util


__author__  = 'ZL <837806295@qq.com>'
__product__ = 'test'
__version__ = '3.2.0'
__date__    = '2023/04/08'


log = logging.getLogger(__product__)


class TestCase001(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.test_sequence = 0
        cls.work_dir = util.get_test_subdir(__file__, cls.__name__)

    @classmethod
    def tearDownClass(cls):
        del cls.test_sequence
        del cls.work_dir

        util.remove_test_subdir(__file__, cls.__name__)

    def setUp(self):
        self.test_sequence = self.__class__.test_sequence
        self.work_dir = self.__class__.work_dir
        self.test_dir = util.get_subdir(
            self.work_dir,
            ('{:0>%d}' % 3).format(self.test_sequence + 1))

    def tearDown(self):
        del self.test_sequence
        del self.work_dir
        del self.test_dir

        self.__class__.test_sequence += 1

    def test_command(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" -h'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py')
                )
        )

        self.assertTrue(util.run(cmd))

class TestCase002(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.test_sequence = 0
        cls.work_dir = util.get_test_subdir(__file__, cls.__name__)

    @classmethod
    def tearDownClass(cls):
        del cls.test_sequence
        del cls.work_dir

        util.remove_test_subdir(__file__, cls.__name__)

    def setUp(self):
        self.test_sequence = self.__class__.test_sequence
        self.work_dir = self.__class__.work_dir
        self.test_dir = util.get_subdir(
            self.work_dir,
            ('{:0>%d}' % 3).format(self.test_sequence + 1))

    def tearDown(self):
        del self.test_sequence
        del self.work_dir
        del self.test_dir

        self.__class__.test_sequence += 1

    def test_subcommand_import(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" import -h'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_import2(self):
        if not sys.executable:
            return

        shutil.copyfile(
            os.path.join(util.get_test_subdir(__file__, ['data']),
                         'test2.pdf'),
            os.path.join(self.test_dir, 'test2.pdf'))

        cmd = (
            '"%s" "%s" import "%s" "%s" -a 0 7'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.test_dir, 'test2.pdf'),
                    os.path.join(
                        util.get_test_subdir(__file__, ['data']),
                        'test2.pdf_outline.txt')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_import3(self):
        if not sys.executable:
            return

        shutil.copyfile(
            os.path.join(util.get_test_subdir(__file__, ['data']),
                         'test2.pdf'),
            os.path.join(self.test_dir, 'test2.pdf'))

        cmd = (
            '"%s" "%s" import "%s" "%s" -a 0 7 -n "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.test_dir, 'test2.pdf'),
                    os.path.join(
                        util.get_test_subdir(__file__, ['data']),
                        'test2.pdf_outline.txt'),
                    os.path.join(
                        util.get_test_subdir(__file__, ['data']),
                        'test2.pdf_replace_rules.txt')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_import4(self):
        if not sys.executable:
            return

        shutil.copyfile(
            os.path.join(util.get_test_subdir(__file__, ['data']),
                         'test2.pdf'),
            os.path.join(self.test_dir, 'test2.pdf'))

        cmd = (
            '"%s" "%s" import "%s" "%s" -a 0 7 -i "type: /Fit" -f 2 '
            '-c 0.0 0.5 0.0 -l debug -L import4'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.test_dir, 'test2.pdf'),
                    os.path.join(
                        util.get_test_subdir(__file__, ['data']),
                        'test2.pdf_outline.txt')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_import5(self):
        if not sys.executable:
            return

        shutil.copyfile(
            os.path.join(util.get_test_subdir(__file__, ['data']),
                         'test2.pdf'),
            os.path.join(self.test_dir, 'test2.pdf'))

        cmd = (
            '"%s" "%s" import "%s" "%s" -a 0 7 -i "type: /Fit" -f 2 '
            '-c 0.0 0.5 0.0 -l debug -L import5'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.test_dir, 'test2.pdf'),
                    util.get_test_subdir(__file__, ['data'])
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_import6(self):
        if not sys.executable:
            return

        shutil.copyfile(
            os.path.join(util.get_test_subdir(__file__, ['data']),
                         'test1.pdf'),
            os.path.join(self.test_dir, 'test1.pdf'))
        shutil.copyfile(
            os.path.join(util.get_test_subdir(__file__, ['data']),
                         'test2.pdf'),
            os.path.join(self.test_dir, 'test2.pdf'))

        cmd = (
            '"%s" "%s" import "%s" "%s" -a 0 7 -i "type: /Fit" -f 2 '
            '-c 0.0 0.5 0.0 -l debug -L import6'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    self.test_dir,
                    os.path.join(
                        util.get_test_subdir(__file__, ['data']),
                        'test2.pdf_outline.txt')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_import7(self):
        if not sys.executable:
            return

        shutil.copyfile(
            os.path.join(util.get_test_subdir(__file__, ['data']),
                         'test1.pdf'),
            os.path.join(self.test_dir, 'test1.pdf'))
        shutil.copyfile(
            os.path.join(util.get_test_subdir(__file__, ['data']),
                         'test2.pdf'),
            os.path.join(self.test_dir, 'test2.pdf'))

        cmd = (
            '"%s" "%s" import "%s" "%s" -a 0 7 -i "type: /Fit" -f 2 '
            '-c 0.0 0.5 0.0 -l debug -L import7'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    self.test_dir,
                    util.get_test_subdir(__file__, ['data'])
                )
        )

        self.assertTrue(util.run(cmd))

class TestCase003(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.test_sequence = 0
        cls.work_dir = util.get_test_subdir(__file__, cls.__name__)

    @classmethod
    def tearDownClass(cls):
        del cls.test_sequence
        del cls.work_dir

        util.remove_test_subdir(__file__, cls.__name__)

    def setUp(self):
        self.test_sequence = self.__class__.test_sequence
        self.work_dir = self.__class__.work_dir
        self.test_dir = util.get_subdir(
            self.work_dir,
            ('{:0>%d}' % 3).format(self.test_sequence + 1))

    def tearDown(self):
        del self.test_sequence
        del self.work_dir
        del self.test_dir

        self.__class__.test_sequence += 1

    def test_subcommand_export(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" export -h'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_export2(self):
        if not sys.executable:
            return

        shutil.copyfile(
            os.path.join(util.get_test_subdir(__file__, ['data']),
                         'test1.pdf'),
            os.path.join(self.test_dir, 'test1.pdf'))

        cmd = (
            '"%s" "%s" export "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.test_dir, 'test1.pdf')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_export3(self):
        if not sys.executable:
            return

        shutil.copyfile(
            os.path.join(util.get_test_subdir(__file__, ['data']),
                         'test1.pdf'),
            os.path.join(self.test_dir, 'test1.pdf'))

        cmd = (
            '"%s" "%s" export "%s" -n "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.test_dir, 'test1.pdf'),
                    os.path.join(util.get_test_subdir(__file__, ['data']),
                                 'test1.pdf_replace_rules.txt')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_export4(self):
        if not sys.executable:
            return

        shutil.copyfile(
            os.path.join(util.get_test_subdir(__file__, ['data']),
                         'test2.pdf'),
            os.path.join(self.test_dir, 'test2.pdf'))

        cmd = (
            '"%s" "%s" export "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(self.test_dir, 'test2.pdf')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_export5(self):
        if not sys.executable:
            return

        shutil.copyfile(
            os.path.join(util.get_test_subdir(__file__, ['data']),
                         'test1.pdf'),
            os.path.join(self.test_dir, 'test1.pdf'))
        shutil.copyfile(
            os.path.join(util.get_test_subdir(__file__, ['data']),
                         'test2.pdf'),
            os.path.join(self.test_dir, 'test2.pdf'))

        cmd = (
            '"%s" "%s" export "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    self.test_dir
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_export6(self):
        if not sys.executable:
            return

        shutil.copyfile(
            os.path.join(util.get_test_subdir(__file__, ['data']),
                         'test1.pdf'),
            os.path.join(self.test_dir, 'test1.pdf'))
        shutil.copyfile(
            os.path.join(util.get_test_subdir(__file__, ['data']),
                         'test2.pdf'),
            os.path.join(self.test_dir, 'test2.pdf'))

        cmd = (
            '"%s" "%s" export "%s" -n "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    self.test_dir,
                    os.path.join(util.get_test_subdir(__file__, ['data']),
                                 'test1.pdf_replace_rules.txt')
                )
        )

        self.assertTrue(util.run(cmd))

class TestCase004(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.test_sequence = 0
        cls.work_dir = util.get_test_subdir(__file__, cls.__name__)

    @classmethod
    def tearDownClass(cls):
        del cls.test_sequence
        del cls.work_dir

        util.remove_test_subdir(__file__, cls.__name__)

    def setUp(self):
        self.test_sequence = self.__class__.test_sequence
        self.work_dir = self.__class__.work_dir
        self.test_dir = util.get_subdir(
            self.work_dir,
            ('{:0>%d}' % 3).format(self.test_sequence + 1))

    def tearDown(self):
        del self.test_sequence
        del self.work_dir
        del self.test_dir

        self.__class__.test_sequence += 1

    def test_subcommand_extract(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" extract -h'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_extract2(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" extract "%s" "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    os.path.join(
                        util.get_test_subdir(__file__, ['data', 'ocr']),
                        'image1.png'),
                    os.path.join(self.test_dir, 'image1.png.txt')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_extract3(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" extract "%s" "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    util.get_test_subdir(__file__, ['data', 'ocr']),
                    os.path.join(self.test_dir, 'ocr.txt')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_extract4(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" extract "%s" "%s" -t .png'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    util.get_test_subdir(__file__, ['data', 'ocr']),
                    os.path.join(self.test_dir, 'ocr.txt')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_extract5(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" extract "%s" "%s" -t .jpg .jpeg'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    util.get_test_subdir(__file__, ['data', 'ocr']),
                    os.path.join(self.test_dir, 'ocr.txt')
                )
        )

        self.assertTrue(util.run(cmd))

class TestCase005(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.test_sequence = 0
        cls.work_dir = util.get_test_subdir(__file__, cls.__name__)

    @classmethod
    def tearDownClass(cls):
        del cls.test_sequence
        del cls.work_dir

        util.remove_test_subdir(__file__, cls.__name__)

    def setUp(self):
        self.test_sequence = self.__class__.test_sequence
        self.work_dir = self.__class__.work_dir
        self.test_dir = util.get_subdir(
            self.work_dir,
            ('{:0>%d}' % 3).format(self.test_sequence + 1))

    def tearDown(self):
        del self.test_sequence
        del self.work_dir
        del self.test_dir

        self.__class__.test_sequence += 1

    def test_subcommand_merge(self):
        if not sys.executable:
            return

        cmd = (
            '"%s" "%s" merge -h'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py')
                )
        )

        self.assertTrue(util.run(cmd))

    def test_subcommand_merge2(self):
        if not sys.executable:
            return

        shutil.copyfile(
            os.path.join(util.get_test_subdir(__file__, ['data']),
                         'test1.pdf'),
            os.path.join(self.test_dir, 'test1.pdf'))
        shutil.copyfile(
            os.path.join(util.get_test_subdir(__file__, ['data']),
                         'test2.pdf'),
            os.path.join(self.test_dir, 'test2.pdf'))

        cmd = (
            '"%s" "%s" merge "%s" "%s"'
                % (
                    sys.executable,
                    os.path.join(util.get_launch_dir(__file__), 'main.py'),
                    self.test_dir,
                    os.path.join(self.test_dir, 'merge.pdf')
                )
        )

        self.assertTrue(util.run(cmd))

class TestCase101(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.test_sequence = 0
        cls.work_dir = util.get_test_subdir(__file__, cls.__name__)

    @classmethod
    def tearDownClass(cls):
        del cls.test_sequence
        del cls.work_dir

        util.remove_test_subdir(__file__, cls.__name__)

    def setUp(self):
        self.test_sequence = self.__class__.test_sequence
        self.work_dir = self.__class__.work_dir
        self.test_dir = util.get_subdir(
            self.work_dir,
            ('{:0>%d}' % 3).format(self.test_sequence + 1))

    def tearDown(self):
        del self.test_sequence
        del self.work_dir
        del self.test_dir

        self.__class__.test_sequence += 1

class TestCase102(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.test_sequence = 0
        cls.work_dir = util.get_test_subdir(__file__, cls.__name__)

    @classmethod
    def tearDownClass(cls):
        del cls.test_sequence
        del cls.work_dir

        util.remove_test_subdir(__file__, cls.__name__)

    def setUp(self):
        self.test_sequence = self.__class__.test_sequence
        self.work_dir = self.__class__.work_dir
        self.test_dir = util.get_subdir(
            self.work_dir,
            ('{:0>%d}' % 3).format(self.test_sequence + 1))

    def tearDown(self):
        del self.test_sequence
        del self.work_dir
        del self.test_dir

        self.__class__.test_sequence += 1

    def test_get_fields_from_line(self):
        line = 'abc'
        field = pdf.get_fields_from_line(line)

        self.assertEqual(len(field), 1)
        self.assertEqual(field[0], 'abc')

        line = '\tabc\tdef\t'
        field = pdf.get_fields_from_line(line)

        self.assertEqual(len(field), 2)
        self.assertEqual(field[0], 'abc')
        self.assertEqual(field[1], 'def')

        line = 'abc\tdef'
        field = pdf.get_fields_from_line(line)

        self.assertEqual(len(field), 2)
        self.assertEqual(field[0], 'abc')
        self.assertEqual(field[1], 'def')

        line = '\t\tabc\t\t\tdef\t\t'
        field = pdf.get_fields_from_line(line)

        self.assertEqual(len(field), 2)
        self.assertEqual(field[0], 'abc')
        self.assertEqual(field[1], 'def')

        line = 'abc\t\t\tdef'
        field = pdf.get_fields_from_line(line)

        self.assertEqual(len(field), 2)
        self.assertEqual(field[0], 'abc')
        self.assertEqual(field[1], 'def')

    def test_get_content_from_lines(self):
        lines = [
            'abc\t3',
            '\tdef\t4',
            '\t\t\txdg\t5',
            'dfef\t6'
        ]

        with self.assertRaises(SystemExit):
            pdf.get_content_from_lines(lines)

        lines = [
            'abc\t3',
            '\tdef\t4',
            '\t\txdg\t5',
            'dfef\t6'
        ]

        content = pdf.get_content_from_lines(lines)
        self.assertTrue(content)

        lines = [
            'abc\t3',
            '\tdef\t4',
            '\t\txdg\t5',
            '\txdg\t8',
            'dfef\t16'
        ]

        content = pdf.get_content_from_lines(lines)
        self.assertTrue(content)

        lines = [
            ''
        ]

        content = pdf.get_content_from_lines(lines)
        self.assertFalse(content)

        lines = [
            '\t'
        ]

        content = pdf.get_content_from_lines(lines)
        self.assertFalse(content)

        lines = []
        content = pdf.get_content_from_lines(lines)
        self.assertFalse(content)


if __name__ == '__main__':
    util.set_log(__file__, '%s_v%s' % (__product__, __version__), 'debug')

    log.info('program version: %s %s', __product__, __version__)
    log.debug('command line: %s', sys.argv)

    unittest.main()
